<?php

namespace Hui\FrontEndBundle\Controller;

use Symfony\Component\HttpFoundation\Session\Session;

use Symfony\Component\HttpFoundation\Response;

use Hui\FrontEndBundle\Form\SearchType;

use Hui\StoreBundle\Entity\ThongTinSanPham;

use Hui\FrontEndBundle\Logic\MenuLogic;

use Hui\FrontEndBundle\Logic\SanPhamLogic;

use Hui\StoreBundle\Entity\MayQuay;

use Hui\StoreBundle\Entity\MayAnh;

use Hui\StoreBundle\Entity\TinTuc;

use Hui\FrontEndBundle\Form\LoaiSanPhamType;

use Hui\StoreBundle\Entity\LoaiSanPham;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class FrontendController extends Controller {
	
	const TYPE_KHUYEN_MAI = '3';
	
	/**
	 * init menu
	 * 
	 * @author anrew
	 * @param unknown $search
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
	public function initMenuAction($search) {
		
		$menuLogic = new MenuLogic($this->getDoctrine()->getManager());
		
		$loaiMayAnhs = $menuLogic->getAllLoaiMayAnh();
		$loaiMayQuays = $menuLogic->getAllLoaiMayQuay();
		$nhaSanXuatMay = $menuLogic->getAllNhaSanXuat();
		$quickSearch;
		if ($search == 0) {
			$quickSearch = 'NULL';
		}
		else {
			$quickSearch = 'NOT NULL';
		}
		return $this->render('HuiFrontEndBundle::left_box.html.twig', array(
				'loaiMayAnhs' => $loaiMayAnhs,
	        	'nhaSanXuatMayAnhs' => $nhaSanXuatMay,
	        	'loaiMayQuays' => $loaiMayQuays,
	        	'nhaSanXuatMayQuays' => $nhaSanXuatMay,
				'quickSearch' => $quickSearch,
		));
		
	}
	
	/**
	 * init gio hang
	 *
	 * @author anrew
	 * @param unknown $search
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
	public function initGioHangAction() {
		$soLuong = 0;
		$request = $this->getRequest();
		$session = $request->getSession();
		if (null != $session->get('cart')) {
			$arrSanPham = $session->get('cart');
			foreach ($arrSanPham as $sanPham) {
				$soLuong += $sanPham['soLuong'];
			}
		}
		
		return $this->render('HuiFrontEndBundle:bundles/frontendbundle:soluongsanpham.html.twig', array(
				'soLuong' => $soLuong,
		));
	
	}
	
	public function initSearchAction() {
		$form = $this->createForm(new SearchType());
		
		return $this->render('HuiFrontEndBundle::search.html.twig', array (
				'form' => $form->createView(),
		));
	}
	
	
	public function searchAction() {
		
		$request = $this->getRequest();
		$form = $this->createForm(new SearchType());
		$form->bindRequest($request);
		$condition = $form->get('condition')->getData();
		$sanPhamLogic = new SanPhamLogic($this->getDoctrine()->getManager());
		$sanPhams = $sanPhamLogic->getSearchResult($condition);
		
		return $this->render('HuiFrontEndBundle:bundles\frontendbundle:sanpham.html.twig', array(
				'sanPhams' => $sanPhams,
				'type' => 4,
		));
	}
	
	/**
	 * index action
	 * 
	 * @author anrew
	 * @return \Symfony\Component\HttpFoundation\Response
	 */
    public function indexAction() {
    	
    	$loaiSanPham = new LoaiSanPham();
    	$form = $this->createForm(new LoaiSanPhamType(), $loaiSanPham);
    	
//     	if ($request->isMethod('POST')) {
//         	$form->bind($request);
//     		if ($form->isValid()) {
    			
//     		}
    					
//    		}
		
		$sanPhamLogic = new SanPhamLogic($this->getDoctrine()->getManager());
		$mayAnhMois = $sanPhamLogic->getAllMayAnhMoi();
		$mayQuayMois = $sanPhamLogic->getAllMayQuayMoi();
		$mayAnhDuois = $sanPhamLogic->getMayAnhDuoi();
		$mayQuayDuois = $sanPhamLogic->getMayQuayDuoi();
		$sanPhamXemNhieus = $sanPhamLogic->getSanPhamXemNhieu();
		
		$tinTucs = array();
		$tinTuc = new TinTuc();
		$tinTuc->setTieuDe('Tiêu đề 1');
		$tinTuc->setTomTat('tóm tắt 1');
		$tinTuc->setNoiDung('nội dung 1');
		$tinTuc->setHinhAnh('');
		array_push($tinTucs, $tinTuc);
		
		$tinTuc = new TinTuc();
		$tinTuc->setTieuDe('Tiêu đề 2');
		$tinTuc->setTomTat('tóm tắt 2');
		$tinTuc->setNoiDung('nội dung 2');
		$tinTuc->setHinhAnh('');
		array_push($tinTucs, $tinTuc);
		
		$tinTuc = new TinTuc();
		$tinTuc->setTieuDe('Tiêu đề 3');
		$tinTuc->setTomTat('tóm tắt 3');
		$tinTuc->setNoiDung('nội dung 3');
		$tinTuc->setHinhAnh('');
		array_push($tinTucs, $tinTuc);
		
		$tinTuc = new TinTuc();
		$tinTuc->setTieuDe('Tiêu đề 4');
		$tinTuc->setTomTat('tóm tắt 4');
		$tinTuc->setNoiDung('nội dung 4');
		$tinTuc->setHinhAnh('');
		array_push($tinTucs, $tinTuc);
   		
        return $this->render('HuiFrontEndBundle:bundles\frontendbundle:index.html.twig', array(
	        	'form' => $form->createView(),
	        	'sanPhamXemNhieus' => $sanPhamXemNhieus,
	        	'mayAnhMois' => $mayAnhMois,
        		'mayQuayMois' => $mayQuayMois,
        		'mayAnhDuois' => $mayAnhDuois,
        		'mayQuayDuois' => $mayQuayDuois,
        		'tinTucs' => $tinTucs,
        		
        ));
    }
    
    /**
     * show the list of mayAnh
     * 
     * @author anrew
     * @param unknown $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function mayAnhCTAction($id) {
    	$sanPhamLogic = new SanPhamLogic($this->getDoctrine()->getManager());
    	$mayAnh = $sanPhamLogic->getMayAnhById($id);
    	return $this->render('HuiFrontEndBundle:bundles\frontendbundle:mayanh.html.twig', array(
    			'mayAnh' => $mayAnh,
    	));
    }
    
    /**
     * show the list of mayQuay
     * 
     * @author anrew
     * @param unknown $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function mayQuayCTAction($id) {
    	$sanPhamLogic = new SanPhamLogic($this->getDoctrine()->getManager());
    	$mayQuay = $sanPhamLogic->getMayQuayById($id);
    	return $this->render('HuiFrontEndBundle:bundles\frontendbundle:mayquay.html.twig', array(
    			'mayQuay' => $mayQuay,
    	));
    }
    
    /**
     * news
     * 
     * @author anrew
     * @param unknown $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function tinTucAction($id) {
    	return $this->render('HuiFrontEndBundle:bundles\frontendbundle:index.html.twig', array(
    			'form' => $form->createView(),
    			'sanPhamXemNhieus' => '',
    	));
    }
    
    /**
     * contact
     * 
     * @author anrew
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function lienHeAction() {
    	return $this->render('HuiFrontEndBundle:bundles\frontendbundle:lienhe.html.twig', array(
    			
    	));
    }
    
    /**
     * show product
     * 
     * @author anrew
     * @param unknown $type
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function sanphamAction($type) {
    	$sanPhamLogic = new SanPhamLogic($this->getDoctrine()->getManager());
    	$sanPhams;
    	if (MayAnh::TYPE_MAY_ANH === $type) {
    		$sanPhams = $sanPhamLogic->getAllMayAnh();
    		$type = MayAnh::TYPE_MAY_ANH;
    		
    	}
    	else if (MayQuay::TYPE_MAY_QUAY === $type) {
    		$sanPhams = $sanPhamLogic->getAllMayQuay();
    		$type = MayQuay::TYPE_MAY_QUAY;
    	}
    	else if (self::TYPE_KHUYEN_MAI === $type) {
    		$sanPhams = $sanPhamLogic->getSanPhamKhuyenMai();
    		$type = self::TYPE_KHUYEN_MAI;
    	}
    	
    	return $this->render('HuiFrontEndBundle:bundles\frontendbundle:sanpham.html.twig', array(
    			'sanPhams' => $sanPhams,
    			'type' => $type,
    	));
    	
    }
    
    /**
     * add product to cart
     *
     * @author anrew
     * @param unknown $type
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function addCartAction($type, $id, $soLuong) {
    	
    	$arrSanPham = array();
    	
    	$total = 0;
    	
    	$sanPham;
    	
    	$request = $this->getRequest();
    	if ($request->getMethod() == 'GET') {
    		return $this->redirect($this->generateUrl('hui_front_end_homepage'));
    	}
    	$session = $request->getSession();
    	
    	$sanPhamLogic = new SanPhamLogic($this->getDoctrine()->getManager());
    	
    	if (null != $session->get('cart')) {
    		$arrSanPham = $session->get('cart');
    	}
    	
    	if ($type == MayAnh::TYPE_MAY_ANH) {
    		$sanPham = $sanPhamLogic->getMayAnhById($id);
    	}
    	else if ($type == MayQuay::TYPE_MAY_QUAY) {
    		$sanPham = $sanPhamLogic->getMayQuayById($id);
    	}
    	
    	$ttSanPham = $sanPham->getThongTinSanPham();
    	
    	array_push($arrSanPham, array(
    			'id' => $id,
    			'tenSp' => $ttSanPham->getTenSp(),
    			'hinhAnh' => $ttSanPham->getHinhAnh(),
    			'soLuong' => $soLuong,
    			'donGia' => $ttSanPham->getGiaBan(),
    	));
    	
//     	if (count($arrSanPham) == 0) {
    		
//     		$total = $soLuong;
//     	}
//     	else {
//     		$flag = true;
	    	foreach ($arrSanPham as $sp) {
	    		
	    		$total += $sp['soLuong'];
	    	}
// 	    	if (!$flag) {
// 	    		array_push($arrSanPham, array(
// 	    			'id' => $id,
// 	    			'tenSp' => $ttSanPham->getTenSp(),
// 	    			'hinhAnh' => $ttSanPham->getHinhAnh(),
// 	    			'soLuong' => $soLuong, 
// 	    		));
// 	    		$total += $soLuong;
// 	    	}
//     	}
    	
    	$session->remove('cart');
    	
    	$session->set('cart', $arrSanPham);
    	
    	
    	$response = new Response(json_encode(array(
    			'result' => 'ok', 
    			'total' => $total,
    	)));
    	$response->headers->set('Content-Type', 'application/json');
    	
    	return $response;
    }
    
    public function showGioHangAction() {
    	$request = $this->getRequest();
    	if ($request->getMethod() == 'GET') {
    		return $this->redirect($this->generateUrl('hui_front_end_homepage'));
    	}
    	$session = $request->getSession();
    	$arrSanPham = array();
    	if (null != $session->get('cart')) {
    		$arrSanPham = $session->get('cart');
    	}
    	
    	$response = new Response(json_encode($arrSanPham));
    	$response->headers->set('Content-Type', 'application/json');
    	
    	return $response;
    } 
}
